package com.aneliya.view;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ScrollPaneConstants;

import com.aneliya.formatter.MessageFormatImpl;
import com.aneliya.view.component.box.VerticalBox;
import com.aneliya.view.component.input.TextInput;
import com.aneliya.view.component.input.TextInputListener;
import com.aneliya.view.component.renderer.TextItem;

public class ChatWindow extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private static final String FRAME_TITLE = "Chat Window";
	
	private static VerticalBox<TextItem> messageVerticalBox;
	private static VerticalBox<TextItem> usersVerticalBox;
	private static TextInput textInput;
	
	protected JScrollPane messagePane;
	protected JScrollPane usersPane;
	protected JSplitPane horizontalSplitPane;
	protected JSplitPane verticalSplitpane;
	
	public ChatWindow() throws Exception {
		initializeGUI();
		getContentPane().add(verticalSplitpane);
		setTitle(FRAME_TITLE);
	}
	
	public void addTextInputListener(TextInputListener listener) {
		textInput.addTextInputListener(listener);
	}
	
	public void removeTextInputListener(TextInputListener listener) {
		textInput.removeTextInputListener(listener);
	}
	
	public void setMessageVerticalBoxData(ArrayList<?> data) {
		messageVerticalBox.setData(data);
	}
	
	public void setUsersVerticalBoxData(ArrayList<?> data) {
		usersVerticalBox.setData(data);
	}
	
	protected void initializeGUI() throws Exception {
		createMessagePane();
		createUsersPane();
		createTextInput();
		
		horizontalSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, messagePane, usersPane);
		horizontalSplitPane.setOneTouchExpandable(true);
		horizontalSplitPane.setDividerLocation(300);
		messagePane.setMinimumSize(new Dimension(150, 100));
		messagePane.setPreferredSize(new Dimension(150, 100));
		usersPane.setMinimumSize(new Dimension(100, 100));
		
		horizontalSplitPane.setBorder(null);
		
		verticalSplitpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, horizontalSplitPane, textInput);
		verticalSplitpane.setOneTouchExpandable(true);
		verticalSplitpane.setDividerLocation(500);	
		horizontalSplitPane.setMinimumSize(new Dimension(250, 100));
		textInput.setMinimumSize(new Dimension(250, 100));
	}
	
	protected void createMessagePane() {
		messageVerticalBox = new VerticalBox<TextItem>();
		messageVerticalBox.setItemDataFormatImpl(new MessageFormatImpl());
		messageVerticalBox.setItemRendererClass(TextItem.class);
		messageVerticalBox.setSize(300, 400);
		
		messagePane = new JScrollPane(messageVerticalBox);
        messagePane.setPreferredSize(new Dimension(300, 400));
        messagePane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        messagePane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);	
	}
	
	protected void createUsersPane() {
		usersVerticalBox = new VerticalBox<TextItem>();
		usersVerticalBox.setItemRendererClass(TextItem.class);
		usersVerticalBox.setSize(200, 400);
		
		usersPane = new JScrollPane(usersVerticalBox);
        usersPane.setPreferredSize(new Dimension(200, 400));
        usersPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        usersPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);	
	}
	
	protected void createTextInput() throws Exception {
		textInput = new TextInput();		
	}	

}
